﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ScreenSharing.sdk;
using ScreenSharing.smartTool;

namespace ScreenSharing
{
    /// <summary>
    /// InitWindow.xaml 的交互逻辑
    /// </summary>
    public partial class InitWindow : Window
    {
        public InitWindow()
        {
            InitializeComponent();
            this.Icon = BitmapToBitmapImage(Properties.Resources.logo); //Properties.Resources.logo

            var roomCode = ConfigurationManager.AppSettings["RoomCode"]??"";
            RoomCode.Text = roomCode;
        }

        private void DoPlay(object sender, RoutedEventArgs e)
        {
            new PlayForm(RoomCode.Text.Trim()).ShowDialog();
        }

        private void DoPublish(object sender, RoutedEventArgs e)
        {

            BaseControll.StartPublish(RoomCode.Text.Trim(),this);
        }

        public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height);
            int i, j;
            for (i = 0; i < bitmap.Width; i++)
            for (j = 0; j < bitmap.Height; j++)
            {
                System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
                System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
                bitmapSource.SetPixel(i, j, newColor);
            }
            MemoryStream ms = new MemoryStream();
            bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
            bitmapImage.EndInit();

            return bitmapImage;
        }
    }
}
